package cn.com.nes.common.util;


import cn.com.nes.mybatis.agent.logs.entity.ImportLogDetails;
import cn.com.nes.site.entity.em.project.ImportLogsTypeEnum;
import cn.com.nes.site.entity.iot.CustomerSurveyExcel;
import cn.com.nes.site.entity.vo.NullVO;
import cn.com.nes.site.entity.vo.project.ImportGovernmentalCustomerArchiveVO;
import cn.com.nes.site.service.logs.ImportLogDetailsService;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * gzip压缩工具类
 *
 * @author nes
 */
public class DistinctByPropertyUtils {
    // 根据身份证号去重
    public static List<CustomerSurveyExcel> distinctByProperty(List<CustomerSurveyExcel> customerList, ImportLogDetailsService importLogDetailsService, NullVO parentVo) {
        Set<String> idSet = new HashSet<>();
        List<CustomerSurveyExcel> distinctList = new ArrayList<>();
        for (int i = 0; i < customerList.size(); i++) {
            CustomerSurveyExcel customer = customerList.get(i);
            customer.setAreaCode(Arrays.asList(customer.getAreaCode().split("_")).get(0));
            //如果身份证为空，那么就给它set一个uuid，这样也是可以添加进去的
            if (!idSet.contains(customer.getIdCardNumber())) {
                if (StringUtils.isEmpty(customer.getIdCardNumber())) {
                    insertDetailsData(importLogDetailsService,customer,parentVo,"身份证号为空，不能添加", ImportLogsTypeEnum.PROJECT_IMPORT.getKey());
                } else {
                    idSet.add(customer.getIdCardNumber());
                }
                    distinctList.add(customer);
            }else{
                insertDetailsData(importLogDetailsService,customer,parentVo,"身份证号重复，不能导入数据",ImportLogsTypeEnum.PROJECT_IMPORT.getKey());
            }

        }
        return distinctList;
    }


    public static void insertDetailsData(ImportLogDetailsService importLogDetailsService,CustomerSurveyExcel item,NullVO parentVo,String message,String type) {
        ImportGovernmentalCustomerArchiveVO vo = (ImportGovernmentalCustomerArchiveVO) parentVo;
        ImportLogDetails importLogDetails = new ImportLogDetails();
        importLogDetails.setCustomerId(vo.getMasterCustomerId());
        importLogDetails.setXzqhdm(item.getAreaCode());
        importLogDetails.setProjectId(vo.getProjectId());
        importLogDetails.setUploadtype(vo.getUploadtype());
        importLogDetails.setOperateUserName(vo.getUserInfo().getNickName());
        importLogDetails.setMessage(message);
        Date date = new Date(vo.getOperateTimeStamp());
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String formattedDate = sdf.format(date);
        importLogDetails.setDrpc(String.valueOf(parentVo.getOperateTimeStamp()));
        importLogDetails.setContactName(item.getContactName());
        importLogDetails.setType(type);
        importLogDetails.setContactPhone(item.getContactPhone());
        importLogDetails.setIdcardNumber(item.getIdCardNumber());
        importLogDetails.setAddress(item.getAddress());
        importLogDetailsService.insertCustomerInfoDetailsData(importLogDetails);
    }
}


